package com.teemor.blog.service.impl;

import com.teemor.blog.bean.condition.TbArticleCondition;
import com.teemor.blog.bean.condition.TbTopicCondition;
import com.teemor.blog.bean.entity.TbArticle;
import com.teemor.blog.bean.entity.TbTopic;
import com.teemor.blog.mapper.TbArticleMapper;
import com.teemor.blog.mapper.TbTopicMapper;
import com.teemor.blog.service.ITopicService;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.teemor.common.bean.utils.enums.DataStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
  * @author: zhoulk
  * @Description: 原子服务实现
  * @date: 2019/11/26 14:50:14
 */
@Service
public class TopicServiceImpl implements ITopicService {

    @Autowired
    private TbTopicMapper tbTopicMapper;
    @Autowired
    private TbArticleMapper articleMapper;

    @Override
    public int countTopic(TbTopicCondition condition) {
        return tbTopicMapper.countTopicByCondition(condition);
    }

    @Override
    public List<TbTopic> qryTopicTree(TbTopicCondition condition) {
        condition.setOrderByClause("pid,topic_id");
        condition.setDataState(DataStatus.USE.value());
        List<TbTopic> tbTopics = tbTopicMapper.selectTopicByCondition(condition);
        if (condition.getTree()==null || condition.getTree() == 1) {
            return tbTopics;
        } else {
            List<TbTopic> result = new ArrayList<>();
            for (TbTopic tbTopic : tbTopics) {
                tbTopic.getChildren().forEach(topic -> topic.setParentName(tbTopic.getTopicName()));
                result.addAll(tbTopic.getChildren());
            }
            return result.stream().sorted(Comparator.comparing(TbTopic::getCreateTime).reversed()).collect(Collectors.toList());
        }
    }

    @Override
    public TbTopic qryTopicById(Integer topicId) {
        return tbTopicMapper.selectTopicByTopicId(topicId);
    }

    @Override
    public int addTopic(TbTopic inParam) {
        inParam.setDataState(DataStatus.USE.value());
        inParam.setCreateTime(new Date());
        tbTopicMapper.insertTopic(inParam);
        return inParam.getTopicId();
    }

    @Override
    public int updTopicById(TbTopic inParam) {
        return tbTopicMapper.updateTopicByTopicId(inParam);
    }

    @Override
    public Map<String, Object> delTopic(Integer topicId) {
        TbTopic topic = new TbTopic();
        topic.setTopicId(topicId);
        topic.setDropTime(new Date());
        topic.setDataState(DataStatus.DELETE.value());
        int num = tbTopicMapper.updateTopicByTopicId(topic);
        TbArticleCondition condition = new TbArticleCondition();
        condition.setTopicId(topicId);
        TbArticle article = new TbArticle();
        article.setAtcId(0);
        condition.setTbArticle(article);
        articleMapper.updateArticleByConditionSelective(condition);
        Map<String, Object> map = new HashMap<>();
        map.put("flag", num > 0);
        return map;
    }
}